from random import random, randint

import pygame
from pygame.sprite import Sprite


class RoleBall(Sprite):
    def __init__(self, screen, radius):
        super(RoleBall, self).__init__()
        self.screen = screen
        self.screen_rect = screen.get_rect()

        # 小球属性
        self.radius = radius
        self.color = (
            randint(0, 255),
            randint(0, 255),
            randint(0, 255),
        )
        # 位置
        self.x = float(randint(self.screen_rect.left + 2 * self.radius, self.screen_rect.right - 2 * self.radius))
        self.y = float(self.screen_rect.top + 2 * self.radius)
        # 移动速度 和方向
        self.speed_y = randint(1, 3)
        self.speed_x = randint(1, 3)

    def update(self):
        self.x += self.speed_x
        self.y += self.speed_y

        # 左右边界
        if self.x - self.radius <= 0:
            self.x = self.radius
            self.speed_x = -self.speed_x
        elif self.x + self.radius >= self.screen_rect.width:
            self.x = self.screen_rect.width - self.radius
            self.speed_x = -self.speed_x

    def draw_role_ball(self):
        pygame.draw.circle(self.screen, self.color, (int(self.x), int(self.y)), self.radius)

    def change_color(self):
        self.color = (randint(0, 255), randint(0, 255), randint(0, 255))
